﻿@page "/credentialconfs"
@inject IState<CredentialConfigurationsState> credentialConfigurationsState
@inject IDispatcher dispatcher
@inject NotificationService notificationService
@inject ContextMenuService contextMenuService
@inject DialogService dialogService
@inject NavigationManager navigationManager
@inject DialogService dialogService
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@using System.Globalization;

<div class="row">
    <div class="col">
        <RadzenText class="mt-3 mb-3 no-margin" Text="@Global.CredentialConfigurationsTitle" TextStyle="TextStyle.DisplayH3" />
        <p>
            @Global.CredentialConfigurationsDescription
        </p>
    </div>
    <div class="col-3">
        <RadzenButton class="mb-1" Click="@(args => AddCredentialConfiguration())" Icon="add" Text="@Global.AddCredentialConfiguration" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
    </div>
</div>

@if(!credentialConfigurationsState.Value.IsLoading)
{
    <RadzenRow Gap="1rem">
        @foreach (var credentialConfiguration in credentialConfigurationsState.Value.CredentialConfigurations)
        {
            <RadzenColumn Size="4">
                <CredentialViewer ServerId="@credentialConfiguration.ServerId" Display="@GetDisplay(credentialConfiguration)">
                    <ActionsContent>
                        <RadzenButton Click=@((evt) => View(credentialConfiguration)) Variant="Variant.Text">@Global.View</RadzenButton>
                        <RadzenButton Click=@((evt) => Remove(credentialConfiguration)) Variant="Variant.Text">@Global.Delete</RadzenButton>
                    </ActionsContent>
                </CredentialViewer>
            </RadzenColumn>
        }
    </RadzenRow>
}
else
{
    <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.Center" Wrap="FlexWrap.Wrap" Class="rz-m-12" Gap="2rem">
        <RadzenProgressBarCircular ShowValue="true" Mode="ProgressBarMode.Indeterminate" Size="ProgressBarCircularSize.Large">
            <Template>@Global.Loading</Template>
        </RadzenProgressBarCircular>
    </RadzenStack>
}

@code {
    bool selectAll = false;
    RadzenDataGrid<Domains.CredentialConfiguration> credentialConfigurationGrid;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            var act = new GetCredentialConfigurationsAction();
            dispatcher.Dispatch(act);
            SubscribeToAction<AddCredentialConfigurationSuccessAction>((act) => 
            {
                notificationService.Notify(NotificationSeverity.Info, Global.CredentialConfigurationAdded);
                StateHasChanged();
            });
            SubscribeToAction<DeleteCredentialConfigurationAction>((act) =>
            {
                notificationService.Notify(NotificationSeverity.Info, Global.CredentialConfigurationRemoved);
                StateHasChanged();
            });
            SubscribeToAction<AddCredentialConfigurationFailureAction>((act) =>
            {
                notificationService.Notify(NotificationSeverity.Error, act.ErrorMessage);
                StateHasChanged();
            });
        }
    }

    void View(Domains.CredentialConfiguration credentialConfiguration)
    {
        navigationManager.NavigateTo($"/credentialconfs/{credentialConfiguration.Id}/settings");
    }

    void Remove(Domains.CredentialConfiguration credentialConfiguration)
    {
        var act = new DeleteCredentialConfigurationAction { Id = credentialConfiguration.Id };
        dispatcher.Dispatch(act);
    }

    async void AddCredentialConfiguration()
    {
        var result = (await dialogService.OpenAsync<CredentialConfigurationModal>(Global.AddCredentialConfigurationTitle)) as Domains.CredentialConfiguration;
        if (result == null) return;
        var act = new AddCredentialConfigurationAction { BaseUrl = result.BaseUrl, Format = result.Format, JsonLdContext = result.JsonLdContext, Scope = result.Scope, Type = result.Type };
        dispatcher.Dispatch(act);
    }

    CredentialConfigurationTranslation GetDisplay(Domains.CredentialConfiguration configuration)
    {
        return configuration.Displays.SingleOrDefault(c => c.Locale == CultureInfo.CurrentUICulture.ToString());
    }
}